package com.freemyleft.left.zapp.selectimage;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;



import com.freemyleft.left.zapp.selectimage.bean.Folder;
import com.freemyleft.left.zapp.selectimage.bean.Image;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/6/5.
 */

public class SystemPhotoUtils {
    private Activity mActivity;
    private static final int LOADER_ALL = 0;
    private boolean hasFolderGened = false;
    private List<Folder> mFolderList = new ArrayList<>();
    private List<Image> mImageList = new ArrayList<>();
    private OnUpdataDataListener mListener;
    public void setOnUpdataDataListener(OnUpdataDataListener listener){
        mListener = listener;
    }

    public SystemPhotoUtils(Activity activity){
        mImageList.clear();
        mFolderList.clear();
        mActivity = activity;
        ((FragmentActivity)mActivity).getSupportLoaderManager().restartLoader(LOADER_ALL, null, mLoaderCallback);
    }
    private LoaderManager.LoaderCallbacks<Cursor> mLoaderCallback = new LoaderManager.LoaderCallbacks<Cursor>() {

        private final String[] IMAGE_PROJECTION = {
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.DATE_ADDED,
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.WIDTH,
                MediaStore.Images.Media.HEIGHT};

        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            CursorLoader cursorLoader = new CursorLoader(mActivity,
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_PROJECTION,
                    null, null, IMAGE_PROJECTION[2] + " DESC");
            return cursorLoader;
//            }else if(id == LOADER_CATEGORY){
//                CursorLoader cursorLoader = new CursorLoader(ImageSelectPhotoActivity.this,
//                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_PROJECTION,
//                        IMAGE_PROJECTION[0]+" like '%"+args.getString("path")+"%'", null, IMAGE_PROJECTION[2] + " DESC");
//                return cursorLoader;
//            }

//            return null;
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            if (data != null) {
                int count = data.getCount();
                if (count > 0) {
                    data.moveToFirst();
                    do {
                        String path = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[0]));
                        String name = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[1]));
                        long dateTime = data.getLong(data.getColumnIndexOrThrow(IMAGE_PROJECTION[2]));
                        int width = data.getInt(data.getColumnIndexOrThrow(IMAGE_PROJECTION[4]));
                        int height =data.getInt(data.getColumnIndexOrThrow(IMAGE_PROJECTION[5]));
                        Image image = new Image(path, name, dateTime, width, height);
                        mImageList.add(image);
                        if (!hasFolderGened) {
                            // 获取文件夹名称
                            File imageFile = new File(path);
                            File folderFile = imageFile.getParentFile();
                            Folder folder = new Folder();
                            folder.name = folderFile.getName();
                            folder.path = folderFile.getAbsolutePath();
                            folder.cover = image;
                            if (!mFolderList.contains(folder)) {
                                List<Image> imageList = new ArrayList<>();
                                imageList.add(image);
                                folder.images = imageList;
                                mFolderList.add(folder);
                            } else {
                                // 更新
                                Folder f = mFolderList.get(mFolderList.indexOf(folder));
                                f.images.add(image);
                            }
                        }

                    } while (data.moveToNext());
                    if (mListener != null){
                        mListener.updateData(mFolderList, mImageList);
                    }
//                    mImageAdapter.setData(images);
//
//                    // 设定默认选择
//                    if(resultList != null && resultList.size()>0){
//                        mImageAdapter.setDefaultSelected(resultList);
//                    }
//
//                    mFolderAdapter.setData(mFolderList);
                    hasFolderGened = true;

                }
            }
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {

        }
    };

    public interface OnUpdataDataListener{
        void updateData(List<Folder> folderList, List<Image> imagelist);
    }
}
